package softwaredepartment.zxit.com.pt_3280cinandroid;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import softwaredepartment.zxit.com.pt_3280cinandroid.Utils.I$IPAddress;

public class EachOtherTalkActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ic_backspace, bt_call;
    private TextView bt_number_1, bt_number_2, bt_number_3, bt_number_4, bt_number_5, bt_number_6,
            bt_number_7, bt_number_8, bt_number_9, bt_number_0, bt_number_dot, bt_number_space, aim_ip_address;

    private String aimIPAddress = "";
    private Button bt_back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_each_other_talk);
        init();

    }

    private void init() {
        aim_ip_address = (TextView) findViewById(R.id.aim_ip_address);
        bt_call = (ImageView) findViewById(R.id.bt_call);
        ic_backspace = (ImageView) findViewById(R.id.ic_backspace);
        bt_number_1 = (TextView) findViewById(R.id.bt_number_1);
        bt_number_2 = (TextView) findViewById(R.id.bt_number_2);
        bt_number_3 = (TextView) findViewById(R.id.bt_number_3);
        bt_number_4 = (TextView) findViewById(R.id.bt_number_4);
        bt_number_5 = (TextView) findViewById(R.id.bt_number_5);
        bt_number_6 = (TextView) findViewById(R.id.bt_number_6);
        bt_number_7 = (TextView) findViewById(R.id.bt_number_7);
        bt_number_8 = (TextView) findViewById(R.id.bt_number_8);
        bt_number_9 = (TextView) findViewById(R.id.bt_number_9);
        bt_number_0 = (TextView) findViewById(R.id.bt_number_0);
        bt_number_dot = (TextView) findViewById(R.id.bt_number_dot);
        bt_number_space = (TextView) findViewById(R.id.bt_number_space);
        bt_back = (Button) findViewById(R.id.bt_back);

        aim_ip_address.setText(aimIPAddress);

        ic_backspace.setOnClickListener(this);
        bt_number_1.setOnClickListener(this);
        bt_number_2.setOnClickListener(this);
        bt_number_3.setOnClickListener(this);
        bt_number_4.setOnClickListener(this);
        bt_number_5.setOnClickListener(this);
        bt_number_6.setOnClickListener(this);
        bt_number_7.setOnClickListener(this);
        bt_number_8.setOnClickListener(this);
        bt_number_9.setOnClickListener(this);
        bt_number_0.setOnClickListener(this);
        bt_number_dot.setOnClickListener(this);
        bt_number_space.setOnClickListener(this);
        bt_call.setOnClickListener(this);
        bt_back.setOnClickListener(this);


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int backspace_width = ic_backspace.getWidth();
        int backspace_heigth = ic_backspace.getWidth();
        LinearLayout.LayoutParams volumeState_prams = new LinearLayout.LayoutParams(backspace_width, backspace_heigth);
        volumeState_prams.gravity = Gravity.CENTER_VERTICAL;
        ic_backspace.setLayoutParams(volumeState_prams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ic_backspace:
                if (aimIPAddress.length() > 0) {
                    aimIPAddress = aimIPAddress.substring(0, aimIPAddress.length() - 1);
                    aim_ip_address.setText(aimIPAddress);
                }

                break;
            case R.id.bt_number_1:
                aimIPAddress += "1";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_2:
                aimIPAddress += "2";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_3:
                aimIPAddress += "3";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_4:
                aimIPAddress += "4";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_5:
                aimIPAddress += "5";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_6:
                aimIPAddress += "6";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_7:
                aimIPAddress += "7";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_8:
                aimIPAddress += "8";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_9:
                aimIPAddress += "9";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_0:
                aimIPAddress += "0";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_dot:
                aimIPAddress += ".";
                aim_ip_address.setText(aimIPAddress);
                break;
            case R.id.bt_number_space:
                break;
            case R.id.bt_call:
                if (I$IPAddress.isIP(aimIPAddress)) {
                    Intent intent = new Intent(EachOtherTalkActivity.this, CallOtherActivity.class);
                    intent.putExtra("AimIPAddress", aimIPAddress);
                    startActivityForResult(intent, 100);
                } else {
                    Snackbar snackbar = Snackbar.make(v, "您输入的IP不符合规范！", Snackbar.LENGTH_LONG);
                    snackbar.getView().setBackgroundColor(getResources().getColor(R.color.red));
                    snackbar.show();
                }
                break;
            case R.id.bt_back:
                finish();
                break;
            default:
                break;


        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode ==100 && resultCode == 100) {
            Toast.makeText(this,"有可能设备不在身边，请稍后再试",Toast.LENGTH_LONG).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
